home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
arrayproc.sim
next >
Wrap
Text File
|
1993-08-16
|
9KB
|
257 lines
begin
% *********************************************
% * *
% * Proposed solution to exercise 6.15: *
% * *
% *********************************************
procedure Min_max_in_array(arr, min_val, max_val);
name min_val, max_val;
integer array arr;
integer min_val, max_val;
begin
integer low_ind, high_ind, ind;
low_ind := lowerbound(arr, 1); high_ind := upperbound(arr, 1);
min_val := maxint; max_val := minint;
for ind := low_ind step 1 until high_ind do
begin
min_val := min(arr(ind), min_val);
max_val := max(arr(ind), max_val);
end;
end of Min_max_in_array;
procedure Min_max_in_2dim_array(arr, min_val, max_val);
name min_val, max_val;
integer array arr;
integer min_val, max_val;
begin
integer low_ind1, high_ind1, low_ind2, high_ind2, ind1, ind2;
low_ind1 := lowerbound(arr, 1); high_ind1 := upperbound(arr, 1);
low_ind2 := lowerbound(arr, 1); high_ind2 := upperbound(arr, 1);
min_val := maxint; max_val := minint;
for ind1 := low_ind1 step 1 until high_ind1 do
for ind2 := low_ind2 step 1 until high_ind2 do
begin
min_val := min(arr(ind1, ind2), min_val);
max_val := max(arr(ind1, ind2), max_val);
end;
end of Min_max_in_2dim_array;
% *********************************************
% * *
% * Proposed solution to exercise 6.17: *
% * *
% *********************************************
procedure Merge(A, B, C, a_high, b_high);
integer array A, B, C;
integer a_high, b_high;
begin
integer a_ind, b_ind, c_ind, next_from_A, next_from_B;
Boolean a_finished, b_finished;
a_ind := 1; next_from_A := A(a_ind); a_finished := false;
b_ind := 1; next_from_B := B(b_ind); b_finished := false;
c_ind := 0;
while not (a_finished and b_finished) do
begin ! a_finished will be true when all elements in A have
! been merged into C, similarily with b_finished.
! At this point, not both a_finished and b_finished.
! There must therefore be at least one element
! in A or B which have not found its place in C.
! This is done as follows: ;
c_ind := c_ind + 1;
if next_from_A < next_from_B then
begin
C(c_ind) := next_from_A;
! Find the next element in A: ;
a_ind := a_ind + 1;
if a_ind le a_high
then next_from_A := A(a_ind)
else begin next_from_A := maxint; a_finished := true end;
end
else begin
C(c_ind) := next_from_B;
! Find the next element in B: ;
b_ind := b_ind + 1;
if b_ind le b_high
then next_from_B := B(b_ind)
else begin next_from_B := maxint; b_finished := true end;
end
end;
end of Merge;
% *********************************************
% * *
% * A procedure that may be used to test the *
% * procedures Min_max_in_array and Merge *
% * *
% *********************************************
procedure test_array_proc(na, nb); integer na, nb;
begin
integer array A(1 : na), B(1 : nb), C(1 : na + nb);
Boolean more_testing;
procedure Give_help;
begin
write_line("Legal commands: ");
write_line(" r: read array ");
write_line(" m: test Merge ");
write_line(" x: test Min_max_in_array ");
write_line(" s: sort array ");
write_line(" w: write arrays");
write_line(" q: quit");
end of give_help;
procedure read_array;
begin character arrchar;
arrchar := prompt_for_char("Read A or B? ");
if arrchar = 'A' then read_arr(A) else
if arrchar = 'B' then read_arr(B);
end;
procedure test_merge;
Merge(A, B, C, na, nb);
procedure test_minmax;
begin character arrchar; integer min_val, max_val;
arrchar := prompt_for_char("Minmax for A, B or C? ");
if arrchar = 'A' then
Min_max_in_array(A, min_val, max_val) else
if arrchar = 'B' then
Min_max_in_array(B, min_val, max_val)
else
Min_max_in_array(C, min_val, max_val);
outtext("Minimum in "); outchar(arrchar); outtext(": "); outint(min_val,0);
outtext(" Maximum: "); outint(max_val,0); outimage;
end of test_findminmax;
procedure sort_array;
begin character arrchar;
arrchar := prompt_for_char("Sort A or B? ");
if arrchar = 'A' then Quicksort(A, 1, na) else
if arrchar = 'B' then Quicksort(B, 1, nb);
end;
procedure write_arrays;
begin
write_array("A", A);
write_array("B", B);
write_array("C", C);
end of write_arrays;
write_line("Testing of Min_max_in_array and Merge:");
Give_help;
more_testing := true;
while more_testing do
begin character c;
c := prompt_for_char("Write command> ");
if c = 'r' then read_array else
if c = 'm' then test_merge else
if c = 'x' then test_minmax else
if c = 's' then sort_array else
if c = 'w' then write_arrays else
if c = 'q' then more_testing := false
else begin write_line("Unknown command"); Give_help end;
end;
end test_array_proc;
% *********************************************
% * *
% * The quicksort-procedure: *
% * *
% *********************************************
procedure Quicksort(table, low_bound, high_bound);
integer array table; integer low_bound, high_bound;
! Sorts the elements in table(low_bound : high_bound) in non-decreasing order;
if low_bound < high_bound then
begin integer some_value, last_below, last_equal, first_above, ind, x;
some_value := table(low_bound);
last_below := low_bound - 1;
last_equal := low_bound;
first_above := high_bound + 1;
ind := low_bound + 1;
while ind < first_above do
begin
x := table(ind);
if x < some_value then
begin
last_below := last_below + 1; last_equal := last_equal + 1;
table(ind) := table(last_below); table(last_below) := x;
ind := ind + 1;
end else
if x = some_value then
begin last_equal := last_equal + 1; ind := ind + 1 end
else begin
first_above := first_above - 1;
table(ind) := table(first_above); table(first_above) := x;
end;
end;
Quicksort(table, low_bound, last_below);
Quicksort(table, first_above, high_bound);
end of Quicksort;
% *********************************************
% * *
% * Some auxiliary procedures: *
% * *
% *********************************************
procedure read_arr(arr);
integer array arr;
begin integer array_length, ind;
array_length := upperbound(arr, 1);
for ind := 1 step 1 until array_length do
arr(ind) := prompt_for_int("Write next array-element> ");
end of read_arr;
procedure write_array(arr_name, arr);
text arr_name; integer array arr;
begin integer arr_length, ind;
arr_length := upperbound(arr, 1);
outtext("The array "); outtext(arr_name);
outtext(" has the following "); outint(arr_length, 0);
outtext(" elements:"); outimage;
for ind := 1 step 1 until arr_length do
outint(arr(ind), 10);
outimage;
end of write_array;
procedure write_line(line); text line;
begin outtext(line); outimage end;
integer procedure prompt_for_int(prompt); text prompt;
begin
outtext(prompt); breakoutimage;
inimage; prompt_for_int := inint;
end;
character procedure prompt_for_char(prompt); text prompt;
begin
outtext(prompt); breakoutimage;
inimage; prompt_for_char := inchar;
end;
% *********************************************
% * *
% * An invocation of the test-procedure: *
% * *
% *********************************************
test_array_proc(6, 6);
end